package com.longrise.android.byjk.plugins.tabfirst.revision.adapter;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.model.Insurace;
import com.longrise.android.byjk.plugins.tabfirst.revision.ConsultationDefaultItem;
import com.longrise.common.utils.PrintLog;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/12/22.
 * 保易健康头条adapter 增加默认情况下的样式
 */

public class ConsultationAdapter extends BaseMultiItemQuickAdapter<ConsultationDefaultItem, BaseViewHolder> {

    private OnItemClickListener mListener;
    private List<ConsultationDefaultItem> mCourseDefaultItemList = new ArrayList<>();

    public ConsultationAdapter(List<ConsultationDefaultItem> data) {
        super(data);
        addItemType(ConsultationDefaultItem.TYPE1,R.layout.by_default_insurance_item_1);    //default 有图片样式
        addItemType(ConsultationDefaultItem.TYPE2,R.layout.by_default_insurance_item_0);    //default 无图片样式
        addItemType(ConsultationDefaultItem.TYPE3,R.layout.item_insurance_1);    //正常样式,无图片
        addItemType(ConsultationDefaultItem.TYPE4,R.layout.item_insurance_0);   //正常样式,有图片
    }

    public void setDatas(List<ConsultationDefaultItem> consultList, boolean isLoadmore) {
        if(isLoadmore){
            List<ConsultationDefaultItem>  list = new ArrayList<>();
            if (consultList != null) {
                list.addAll(consultList);
            }
            mCourseDefaultItemList.addAll(list);
            PrintLog.e(TAG,"mCourseDefaultItemList=:"+mCourseDefaultItemList.toString());
        }else{
            mCourseDefaultItemList.clear();
            if (consultList != null) {
                mCourseDefaultItemList.addAll(consultList);
            }
            PrintLog.e(TAG,"mCourseDefaultItemList=:"+mCourseDefaultItemList.toString());
        }
        setNewData(mCourseDefaultItemList);
    }

    @Override
    protected void convert(BaseViewHolder helper, ConsultationDefaultItem consultationDefaultItem) {
        final int layoutPosition = helper.getLayoutPosition();
        final Insurace item = consultationDefaultItem.getInsutaceChildBean();
        final View convertView = helper.getConvertView();
        if(consultationDefaultItem.TYPE1 == consultationDefaultItem.getItemType()){

        }else if(consultationDefaultItem.TYPE2 == consultationDefaultItem.getItemType()){

        }else if(consultationDefaultItem.TYPE3 == consultationDefaultItem.getItemType()){
            setData2View(item,helper,convertView,layoutPosition);
        }else if(consultationDefaultItem.TYPE4 == consultationDefaultItem.getItemType()){
            setPicData2View(item,helper,convertView,layoutPosition);
        }

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onClick(layoutPosition,item);
                }
            }
        });
    }

    //设置没有图片的数据
    private void setData2View(Insurace item, BaseViewHolder helper, final View convertView, int layoutPosition) {
        //------标题---------
        String name = item.getTitle();
        final TextView tvTitle = helper.getView(R.id.tvtitle_insurance_1);
        tvTitle.setText(name == null ? "" : name);


        //新闻来源
        String newfrom = item.getNewfrom();
        TextView tvNewfrom = helper.getView(R.id.tvfrom_insurance_1);
        tvNewfrom.setText(newfrom);

        //评论数
        String comment = item.getReviewnumstr();
        TextView tvComment = helper.getView(R.id.tvconment_insurance_1);
        tvComment.setText(comment + "评论");

        //创建时间
        String createTime = item.getCreatetime();
        TextView tvTextTime = helper.getView(R.id.tvtime_insurance_1);
        tvTextTime.setText(createTime);

        //让最后的item 分割线不显示
        if (layoutPosition == mCourseDefaultItemList.size()) {
            helper.getView(R.id.item_insurance_divider1).setVisibility(View.GONE);
        } else {
            helper.getView(R.id.item_insurance_divider1).setVisibility(View.VISIBLE);
        }
    }

    //设置有图片的数据
    private void setPicData2View(Insurace item, BaseViewHolder helper, final View convertView,int layoutPosition) {
        //图片
        ImageView imageView = helper.getView(R.id.ivpic_insurance_0);
        String realpic = item.getNewspic();
        Glide.with(mContext)
                .load(realpic)
                .placeholder(mContext.getResources().getDrawable(R.drawable.img03))
                .crossFade()
                .into(imageView);
        Glide.get(mContext).clearMemory();
        //------标题---------
        String name = item.getTitle();
        final TextView tvTitle = helper.getView(R.id.tvtitle_insurance_0);
        tvTitle.setText(name == null ? "" : name);


        //新闻来源
        String newfrom = item.getNewfrom();
        TextView tvNewfrom = helper.getView(R.id.tvfrom_insurance_0);
        tvNewfrom.setText(newfrom);

        //评论数
        String comment = item.getReviewnumstr();
        TextView tvComment = helper.getView(R.id.tvconment_insurance_0);
        tvComment.setText(comment + "评论");

        //创建时间
        String createTime = item.getCreatetime();
        TextView tvTextTime = helper.getView(R.id.tvtime_insurance_0);
        tvTextTime.setText(createTime);

        //让最后的item 分割线不显示
        if (layoutPosition == mCourseDefaultItemList.size()) {
            helper.getView(R.id.item_insurance_divider0).setVisibility(View.GONE);
        } else {
            helper.getView(R.id.item_insurance_divider0).setVisibility(View.VISIBLE);
        }
    }



    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    public interface OnItemClickListener {
        void onClick(int position,Insurace item);
    }
}
